// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "map863.h"

#include "MainFrm.h"
#include "map863View.h"
#include "DialogOption.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	ON_WM_CREATE()
	ON_COMMAND(ID_TOOLS_OPTION, &CMainFrame::OnToolsOption)
//	ON_WM_LBUTTONUP()
//	ON_WM_ERASEBKGND()
//	ON_UPDATE_COMMAND_UI(ID_DRAW_PATH, &CMainFrame::OnUpdateDrawPath)
//	ON_COMMAND(ID_DRAW_PATH, &CMainFrame::OnDrawPath)
ON_COMMAND(ID_TOOLS_DEMO, &CMainFrame::OnToolsDemo)
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
//	ID_INDICATOR_SCRL,
};


// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
}

CMainFrame::~CMainFrame()
{
}


int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	// TODO: Delete these three lines if you don't want the toolbar to be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);

	
//	if (!m_wndTabBar.Create(this, IDD_DIALOG_FLATTAB,  CBRS_TOP, IDD_DIALOG_FLATTAB) )
//	{ 
//		TRACE0("Failed to create Dialog Bar\n");
//		return -1;      // fail to create
//	}
//	m_wndTabBar.GetDlgItem(IDC_FLATTAB_EDIT1)->SetWindowText(_T("my dialog bar"));
//	((CComboBox *)m_wndTabBar.GetDlgItem(IDC_COMBO1))->AddString(_T("my dialog bar"));
//	CTabCtrl * m_wndTab =((CTabCtrl *) m_wndTabBar.GetDlgItem(IDC_TAB2));
	
	//Set toolbar icons
	m_ToolsIcon.Create(48, 48, ILC_COLOR32 | ILC_MASK, 20, 0);
	m_ToolsIcon.Add(theApp.LoadIcon(IDI_ICON_FILENEW));   
	m_ToolsIcon.Add(theApp.LoadIcon(IDI_ICON_FILEOPEN));  
	m_ToolsIcon.Add(theApp.LoadIcon(IDI_ICON_FILESAVE)); 	 
	m_ToolsIcon.Add(theApp.LoadIcon(IDI_ICON_COPY)); 
	m_ToolsIcon.Add(theApp.LoadIcon(IDI_ICON_CUT)); 
	m_ToolsIcon.Add(theApp.LoadIcon(IDI_ICON_PASTE)); 
	m_ToolsIcon.Add(theApp.LoadIcon(IDI_ICON_DELETE)); 
	m_ToolsIcon.Add(theApp.LoadIcon(IDI_ICON_DRAWLINE)); 
	m_ToolsIcon.Add(theApp.LoadIcon(IDI_ICON_ZOOMIN)); 
	m_ToolsIcon.Add(theApp.LoadIcon(IDI_ICON_ZOOM100)); 
	m_ToolsIcon.Add(theApp.LoadIcon(IDI_ICON_ZOOMOUT)); 
	m_ToolsIcon.Add(theApp.LoadIcon(IDI_ICON_LAYER)); 
	m_ToolsIcon.Add(theApp.LoadIcon(IDI_ICON_CAMERA)); 
	m_ToolsIcon.Add(theApp.LoadIcon(IDI_ICON_BENET)); 
	m_ToolsIcon.Add(theApp.LoadIcon(IDI_ICON_DOWNLOAD)); 
	m_ToolsIcon.Add(theApp.LoadIcon(IDI_ICON_DEMO)); 
	m_wndToolBar.GetToolBarCtrl().SetImageList(&m_ToolsIcon);

	//add drop down items to toolbar
	DWORD dwExStyle = TBSTYLE_EX_DRAWDDARROWS;
	m_wndToolBar.GetToolBarCtrl().SendMessage(TB_SETEXTENDEDSTYLE, 0, (LPARAM)dwExStyle);
	DWORD dwStyle = m_wndToolBar.GetButtonStyle(m_wndToolBar.CommandToIndex(ID_FILE_OPEN));
	dwStyle |= TBSTYLE_DROPDOWN;
	m_wndToolBar.SetButtonStyle(m_wndToolBar.CommandToIndex(ID_DRAW_LAYER), dwStyle);

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	cs.style = WS_OVERLAPPED | WS_CAPTION | FWS_ADDTOTITLE
		 | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SIZEBOX | WS_SYSMENU;

	return TRUE;
}


// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG


// CMainFrame message handlers




void CMainFrame::OnToolsOption()
{
	// TODO: Add your command handler code here
   Cmap863Doc*  pDoc=(Cmap863Doc*)GetActiveDocument();
   Cmap863View* pView=(Cmap863View*)GetActiveView();

	CDialogOption DialogOption;
	//Get Color
	for (int i=0;i<15;i++)
	{
		pView->ViewPen[i].GetLogPen(&DialogOption.DialogSetColor.LogPen[i]);
	}
	//Get font
	pView->ViewFont.GetLogFont(&DialogOption.DialogSetColor.TextLogFont);
	//Get EXE file path
	DialogOption.DialogSetPath.path1=pView->m_CameraProgramPath;
	DialogOption.DialogSetPath.path2=pView->m_BEProgramPath;
	//Get background bmp file path
	DialogOption.DialogSetBG.m_ImagePath=pDoc->BGImagePath;
		TRACE(_T("File path in view is:"));
		TRACE(pView->m_CameraProgramPath+pView->m_BEProgramPath);
		TRACE(_T("\n"));
	//Get baground size
		DialogOption.DialogSetBG.BGWidth=pDoc->BGWidth;
		DialogOption.DialogSetBG.BGHeight=pDoc->BGHeight;

	if (DialogOption.DoModal()==IDOK)
	{
		//Update color
		for (int i=0;i<15;i++)
		{
			pView->ViewPen[i].DeleteObject();
			pView->ViewPen[i].CreatePenIndirect(&DialogOption.DialogSetColor.LogPen[i]);
		}
		//Update font
		pView->ViewFont.DeleteObject();
		pView->ViewFont.CreateFontIndirect(&DialogOption.DialogSetColor.TextLogFont);
		//Update background image
		if (GetFileAttributes(DialogOption.DialogSetBG.m_ImagePath)!=-1)
		{
			pDoc->BGImagePath=DialogOption.DialogSetBG.m_ImagePath;
			pView->m_hBackgroundImage=(HBITMAP)LoadImage(NULL,pDoc->BGImagePath, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);
			pView->m_BackgroundImage.Detach();		
			pView->m_BackgroundImage.Attach(pView->m_hBackgroundImage);
			pDoc->BGWidth=DialogOption.DialogSetBG.BGWidth;
			pDoc->BGHeight=DialogOption.DialogSetBG.BGHeight;
		}
		//Update calibration file path
		pView->m_CameraProgramPath=DialogOption.DialogSetPath.path1;
		pView->m_BEProgramPath=DialogOption.DialogSetPath.path2;	
		TRACE(_T("File path in Option is:"));
		TRACE(DialogOption.DialogSetPath.path1+DialogOption.DialogSetPath.path2);
		TRACE(_T("\n"));
	}

	pView->Invalidate();

}


void CMainFrame::OnToolsDemo()
{
	// TODO: Add your command handler code here
}
